import { _decorator, Component, Node, input, Input, EventTouch, Collider, ITriggerEvent } from 'cc'
import { Constant } from '../framework/Constant'
import { GameManager } from '../framework/GameManager'
const { ccclass, property } = _decorator

@ccclass('MyPlane')
export class MyPlane extends Component {
  @property(GameManager)
  gameManager: GameManager = null

  onEnable() {
    // 获取碰撞组件
    const collider = this.getComponent(Collider)
    // 监听开始碰撞
    collider.on('onTriggerEnter', this.onTriggerEnter, this)

    // 监听接触停留
    collider.on('onTriggerStay', this.onTriggerEnter, this)
  }

  onDisable() {
    const collider = this.getComponent(Collider)
    collider.off('onTriggerEnter', this.onTriggerEnter, this)
    collider.off('onTriggerStay', this.onTriggerEnter, this)
  }
  // 碰撞处理
  onTriggerEnter(event: ITriggerEvent) {
    const group = event.otherCollider.getGroup()
    if (group === Constant.collisionGroup.ENEMY_PLANE || group === Constant.collisionGroup.ENEMY_BULLET) {
      // console.log('飞机被击中')
      this.gameManager.bloodReduction()
    }
    if (group === Constant.collisionGroup.BULLET_PROP) {
      // console.log('吃到道具')
    }
  }
}
